#include "chatserver.hpp"      //可能路径配置
#include "json.hpp"
#include<string>
#include<functional>
#include "chatservice.hpp"
using namespace std;
using namespace placeholders;
using json=nlohmann::json;


ChatServer::ChatServer(EventLoop* loop,
    const InetAddress& listenAddr,
    const string& nameArg)
    :_server(loop,listenAddr,nameArg),_loop(loop)
{
    //注册链接回调
    _server.setConnectionCallback(std::bind(&ChatServer::onConnection,this,_1));
    //注册消息回调
    _server.setMessageCallback(std::bind(&ChatServer::onMessage,this,_1,_2,_3));
    //设置线程数量
    _server.setThreadNum(4);
}

void ChatServer:: start()
{

}

//上报链接相关信息的回调函数
void ChatServer::onConnection(const TcpConnectionPtr&conn)
{
    //客户端断开
    if(!conn->connected())
    {
        conn->shutdown();
    }

}

//上报读写事件相关信息的回调函数
void ChatServer::onMessage(const TcpConnectionPtr&conn,
        Buffer*buffer,
        Timestamp time)
{
    string buf=buffer->retrieveAllAsString();
//数据反序列化
    json js =json::parse(buf);
    //达到的目的：完全解耦网络模块的代码业务模块
    //通过js["msgid"]获取-》业务handlder-》conn js time
    //js.get()为将一个js[]类型转换为int类型
    auto msgHandler=ChatService::instance()->getHandler(js["msgid"].get<int>());
    //回调消息绑定好的事件处理器，来执行相应的业务处理
    msgHandler(conn,js,time);
    

}

    

